-- 刷怪逻辑
logic = logic or {}

local function removeIfDead(whichGroup)
    if (#whichGroup > 0) then
        for idx, eu in ipairs(whichGroup) do
            if hunit.getCurLife(eu) <= 0 then
                table.remove(whichGroup, idx)
                idx = idx - 1
            end
        end
    end
end

logic.spawn = {
    --- @param wave number 第几波怪
    --- @param count number 刷怪数量
    --- @param interval number 刷怪间隔
    --- @param x number x坐标
    --- @param y number y坐标
    --- @public
    attacker = function(wave, count, interval, x, y)

        local counter = 0
        htime.setInterval(
                interval,
                function(curTimer)
                    counter = counter + 1
                    if (counter > count) then
                        htime.delTimer(curTimer)
                    else
                        -- print_mb(hslk_global.name2Value.unit["第" .. wave .. "波"].UNIT_ID)
                        local u = henemy.create(
                                {
                                    unitId = jzjh_global.attacker[wave].id,
                                    x = x,
                                    y = y
                                }
                        )
                        if #jzjh_global.attackTechs > 0 then
                            for i = 1, #jzjh_global.attackTechs do
                                hskill.add(u, jzjh_global.attackTechs[i])
                            end
                        end
                        order.attackMoveTo(u, CONST_POINT.base.x, CONST_POINT.base.y)
                    end
                end
        )
    end,
    finalAttackBoss = function(x, y)
        local u = henemy.create(
                {
                    unitId = jzjh_global.attackBoss[#jzjh_global.attackBoss].id,
                    x = x,
                    y = y
                }
        )
        order.attackMoveTo(u, CONST_POINT.base.x, CONST_POINT.base.y)
    end,
    attackBoss = function(wave, x, y)
        local u = henemy.create(
                {
                    unitId = jzjh_global.attackBoss[wave // 3].id,
                    x = x,
                    y = y
                }
        )
        order.attackMoveTo(u, CONST_POINT.base.x, CONST_POINT.base.y)
    end,
    --- @param unitId string 单位ID
    --- @param x number x坐标
    --- @param y number y坐标
    --- @param reviveTime number 复活时间
    instance = function(unitId, x, y, reviveTime)
        local u = henemy.create({
            unitId = unitId,
            x = x,
            y = y
        })
        hevent.onDead(u, function(evtData)
            htime.setTimeout(reviveTime, function(curTimer)
                logic.spawn.instance(hunit.getId(evtData.triggerUnit), x, y, reviveTime)
            end)
        end)
    end,
    -- 练功房刷怪
    training = function()
        removeIfDead(jzjh_global.leftTrainingGroup)
        removeIfDead(jzjh_global.rightTrainingGroup)
        local wave = jzjh_global.wave < 1 and 1 or jzjh_global.wave
        if hgroup.count(jzjh_global.leftTrainingGroup) <= 3 then
            local g = hunit.create({
                whichPlayer = jzjh_global.trainPlayer,
                unitId = jzjh_global.attacker[wave].id,
                x = 12195.0,
                y = -4335.0,
                qty = 10,
            })
            jzjh_global.leftTrainingGroup = table.merge(jzjh_global.leftTrainingGroup, g)
        end
        if hgroup.count(jzjh_global.rightTrainingGroup) <= 3 then
            local g = hunit.create({
                whichPlayer = jzjh_global.trainPlayer,
                unitId = jzjh_global.attacker[wave].id,
                x = 15255.0,
                y = -4342.0,
                qty = 10,
            })
            jzjh_global.rightTrainingGroup = table.merge(jzjh_global.rightTrainingGroup, g)
        end
    end
}
